from django.contrib.auth import login, authenticate
from django.shortcuts import render, HttpResponse, redirect
from .models import *
from django.core.paginator import Paginator

# Create your views here.
from app.models import Yonghu


def register(request):
    if request.method == 'GET':
        return render(request, 'register.html')
    if request.method == 'POST':
        uname = request.POST.get('uname')
        upwd = request.POST.get('upwd')
        role = request.POST.get('role')
        Yonghu.objects.create(uname=uname, upwd=upwd, role=role)
        return redirect('dengLu')


def dengLu(request):
    if request.method == 'GET':
        return render(request, 'dengLu.html')
    if request.method == 'POST':
        uname = request.POST.get('uname')
        upwd = request.POST.get('upwd')
        user = Yonghu(uname=uname, upwd=upwd)
        if user:
            return redirect('index')
        else:
            return HttpResponse('登录失败，重新登录')


def index(request):
    a = Qingjiashenpi.objects.all()
    page = request.GET.get('page', '1')
    p = Paginator(a, '1')
    sum = p.num_pages
    nowpage = p.get_page(page)
    nextpage = nowpage.number + 1
    lastpage = nowpage.number - 1
    return render(request,'index.html',{'sum':sum,'nowpage':nowpage,
                                        'nextpage':nextpage,'lastpage':lastpage})




def add(request):
    if request.method == 'GET':
        return render(request, 'add.html')
    if request.method == 'POST':
        stu_name = request.POST.get('stu_name')
        tname = request.POST.get('tname')
        context=request.POST.get('context')
        createtime = request.POST.get('createtime')
        leixing = request.POST.get('leixing')
        Qingjiashenpi.objects.create(context=context,stu_name=stu_name,tname=tname,createtime=createtime,leixing=leixing)
        return redirect('index')
